package com.stylefeng.guns.rest.Tools;

import com.stylefeng.guns.core.base.tips.Tip;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author 张亚林
 * @Description //通用返回结果集
 * @Date 2018-09-07
 **/
@Data
@ApiModel(value="返回结果集",description = "系统统一返回结果集")
@NoArgsConstructor
@AllArgsConstructor
public class Result {
    private String message;
    private int code;
    private Object data;
    private Tip tip;
    private Result(Object data,int code) {
        this.code = code;
    switch (code) {
      case 200:
        this.message = "操作成功";
        break;
       case 400:
           this.message="操作失败";
           break;
        case 600:
            this.message="重新登陆";
            break;
         default:
            this.message="操作成功";
            break;
    }
        this.data = data;
    }
    private Result(Object data,String message,int code){
        this.data=data;
        this.message=message;
        this.code=code;

    }
    private Result(String message,int code){
        this.message=message;
        this.code=code;

    }
    private Result(CodeMsg cm) {
        if(cm == null){
            return;
        }
        this.code = cm.getRetCode();
        this.message = cm.getMessage();
    }

    private Result(Tip tip) {
        if(tip == null){
            return;
        }
        this.code = tip.getCode();
        this.message = tip.getMessage();
    }
    /**
     * 成功时候的调用
     * @return
     */
    public static  Result success(Object data){
        return new Result(data,200);
    }

    /**
     * 成功时候的调用
     * @return
     */
    public static  Result success(Object data,String message){
        return new Result(data,message,200);
    }
    /**
     * 成功时候的调用
     * @return
     */
    public static  Result success(String message,int code){
        return new Result(message,200);
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static  Result error(Object data){
        return new Result(data,400);
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static  Result error(Object data,String message,int code){
        return new Result(data,message,code);
    }

    /**
     * 失败时候的调用
     * @return
     */
    public static  Result error(String message,int code){
        return new Result(message,code);
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static  Result relogin(Object data){
        return new Result(data,600);
    }
    /**
     * 成功，不需要传入参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public static  Result success(){
        return (Result) success("");
    }

}
